const getFormulaLabelMap = (instance) => window.formulaLabelMap ? window.formulaLabelMap : typeof settingParam == "undefined" ? {} : eval("settingParam").formulaLabelMap ? eval("settingParam").formulaLabelMap : {}, getCurrentFormFormulaLabelMap = (instance) => typeof settingParam == "undefined" ? {} : eval("settingParam").currentFormFormulaLabelMap ? eval("settingParam").currentFormFormulaLabelMap : {};
function createSpan(t, n, i) {
  var o = document.createElement("span");
  return o.setAttribute("title", "" + t), o.setAttribute("class", "CodeMirror-widget cm-field-" + i), o.setAttribute("data-field", t), window.isYijingWechat && i == "User" ? o.innerHTML = '<span class="cm-field cm-field-' + i + `"><ww-open-data type='userName' openid =` + n + "></ww-open-data></span>" : window.isYijingWechat && i == "Department" ? o.innerHTML = '<span class="cm-field cm-field-' + i + `"><ww-open-data type='departmentName' openid =` + n + "></ww-open-data></span>" : o.innerHTML = '<span class="cm-field cm-field-' + i + '">' + n + "</span>", o;
}
window.initFormulaEditor = function(t, n) {
  var i = [], o = [];
  if (t) {
    let d = [t];
    const r = getFormulaLabelMap(), s = getCurrentFormFormulaLabelMap();
    forEach(d, function(c, A) {
      var f = "", N = splitFunc(A);
      forEach(N, function(z, e) {
        if (isNull(e))
          return;
        var a, u = "BOITEMNAME", l;
        if (/^(@form\([0-9a-zA-Z._]+,[0-9a-zA-Z._]+\))/.test(e)) {
          if (l = e.replace("@form(", "").replace(")", "").replace(",", "."), r && (a = r[l]), isNull(a)) {
            f += e;
            return;
          }
        } else if (/^(%_[0-9a-zA-Z._]+_%)/.test(e)) {
          if (l = e.replace("%_", "").replace("_%", ""), r && (a = r[l]), isNull(a)) {
            f += e;
            return;
          }
          u = "common_field";
        } else if (/^\\,/.test(e))
          a = ",";
        else if (/^\\\(/.test(e))
          a = "(";
        else if (/^\\\)/.test(e))
          a = ")";
        else if (/^\\\\/.test(e))
          a = "\\";
        else if (/^(\$\[[0-9a-zA-Z._]+\])/.test(e)) {
          l = e.replace("$[", "").replace("]", "");
          let p = n.getOption("contextData");
          if (typeof p != "undefined" && typeof p.data != "undefined" && Array.isArray(p.data))
            for (var h = p.data, _ = 0; _ < h.length; _++) {
              var b = h[_];
              if (typeof b == "string" && b.indexOf("$_BO_") > -1) {
                var g = b.replace("$_BO_", "");
                if (g != null && r[g] != null && r[g][l] != null) {
                  a = r[g][l];
                  break;
                }
              }
            }
          if (typeof a == "undefined" && s && (a = s[l]), typeof a == "undefined" && r && (a = r[l]), isNull(a)) {
            f += e;
            return;
          }
          a = "$[" + a + "]", u = "source";
        } else if (/^(#\[[0-9a-zA-Z._]+\])/.test(e)) {
          l = e.replace("#[", "").replace("]", "");
          let p = n.getOption("contextData");
          if (typeof p != "undefined" && typeof p.data != "undefined" && Array.isArray(p.data))
            for (var v = p.data, m = 0; m < v.length; m++) {
              var M = v[m];
              if (typeof M == "string" && M.indexOf("#_BO_") > -1) {
                let w = M.replace("#_BO_", "");
                if (w != null && r[w] != null && r[w][l] != null) {
                  a = r[w][l];
                  break;
                }
              }
            }
          if (typeof a == "undefined" && (l = e.replace("#[", "").replace("]", ""), r && (a = r[l])), isNull(a)) {
            f += e;
            return;
          }
          a = "#[" + a + "]", u = "target";
        } else if (/^([0-9a-zA-Z._]+,[0-9a-zA-Z._]+)/.test(e)) {
          if (l = e.replace(",", "."), r && (a = r[l]), isNull(a)) {
            f += e;
            return;
          }
          u = "BOITEMNAME_PARAM";
        } else if (/^(@processVar\([\u4e00-\u9fa5_a-zA-Z0-9]+\))/.test(e)) {
          if (a = e.replace("@processVar(", "").replace(")", ""), isNull(a)) {
            f += e;
            return;
          }
          u = "common_field";
        } else if (/^(@env\([\u4e00-\u9fa5_a-zA-Z0-9]+[\,(ext1|ext2|ext3)]*\))/.test(e)) {
          if (a = e.replace("@env(", "").replace(")", ""), isNull(a)) {
            f += e;
            return;
          }
          u = "ccVar";
        } else if (/^(@[uid|companyId|departmentId|roleId|teamId]+\([0-9,A-z._-]+\))/.test(e)) {
          if (l = e, r && (a = r[l]), isNull(a)) {
            f += e;
            return;
          }
          e.indexOf("uid") > -1 ? u = "User" : e.indexOf("companyId") > -1 ? u = "Company" : e.indexOf("departmentId") > -1 ? u = "Department" : e.indexOf("roleId") > -1 ? u = "Role" : e.indexOf("teamId") > -1 && (u = "Team");
        } else if (/^([0-9a-zA-Z_]+.[0-9a-zA-Z_]+)/.test(e)) {
          if (l = e, r && (a = r[l]), isNull(a) || e == "AVG(*boName.fieldName)" || e == "MIN(*boName.fieldName)" || e == "SUM(*boName.fieldName)" || e == "COUNT(*boName)" || e == "MAX(*boName.fieldName)") {
            f += e;
            return;
          }
          a = "$[" + a + "]", u = "source";
        } else {
          f += e;
          return;
        }
        let y = window.CodeMirror.Pos(c, f.length);
        f += e;
        var m = window.CodeMirror.Pos(c, f.length);
        o.push({ from: y, to: m, field: e, invalid: u, label: a });
      }), i.push(f);
    });
  }
  n.setValue(i.join(`
`)), forEach(o, function(d, r) {
    markField(r, n);
  });
};
function markField(t, n) {
  n.markText(t.from, t.to, {
    handleMouseEvents: !0,
    atomic: !0,
    replacedWith: createSpan(t.field, t.label, t.invalid)
  }), window.CodeMirror.signal(n.doc.cm, "endCompletion", n.doc.cm), bindAll();
}
function bindAll() {
  window.WechatSettime && clearTimeout(window.WechatSettime), window.WechatSettime = setTimeout(function() {
    const t = document.querySelectorAll("ww-open-data");
    self.frameElement && self.frameElement.tagName == "IFRAME" ? window.top.window.WWOpenData.bindAll(t) : window.WWOpenData.bindAll(t);
  }, 100);
}
function forEach(t, n) {
  if (Array.isArray(t))
    for (var i = 0, o = t.length; i < o && n.apply(t[i], [i, t[i]]) !== !1; i++)
      ;
  else if (t && typeof t == "object") {
    for (var d in t)
      if (Object.prototype.hasOwnProperty.call(t, d) && n.apply(t[d], [d, t[d]]) === !1)
        break;
  }
}
function isNull(t) {
  return t == null;
}
function splitFunc(t) {
  let n = /(@form\([0-9a-zA-Z._]+,[0-9a-zA-Z._]+\))|(%_[0-9a-zA-Z._]+_%)|(\$\[[0-9a-zA-Z._]+\])|(#\[[0-9a-zA-Z._]+\])|(@form\(|@gridFirst\(|@gridLast\(|@gridSum\(|@gridAvg\(|@gridMax\(|@gridMin\()+([0-9a-zA-Z._]+,[0-9a-zA-Z._]+(?=,|\)))|(\\,|\\\(|\\\)|\\\\)|(@processVar\([\u4e00-\u9fa5_a-zA-Z0-9]+\))|(@env\([\u4e00-\u9fa5_a-zA-Z0-9]+[\,(ext1|ext2|ext3)]*\))|(@[uid|companyId|departmentId|roleId|teamId]+\([0-9,A-z._-]+\))|([0-9a-zA-Z_]+\.[0-9a-zA-Z_]+)/g;
  if (window.navigator.userAgent.indexOf("MSIE") > -1 || window.navigator.userAgent.indexOf("Trident") > -1) {
    let i = [], o = t.match(n);
    if (o.length == 0)
      i.push(t);
    else {
      let d = t;
      for (let r of o)
        d.replace(r, (s, c, A) => {
          c > 0 && i.push(A.substring(0, c)), i.push(r), d = A.substring(c + s.length);
        });
      d.length > 0 && i.push(d);
    }
    return i;
  } else
    return t.split(n).filter(Boolean);
}
export {
  getCurrentFormFormulaLabelMap,
  getFormulaLabelMap
};
